package com.cx.api.controller;

import com.alibaba.fastjson.JSON;
import com.cx.api.holder.LoginUserHolder;
import com.cx.api.model.SysUser;
import com.cx.api.service.TestService;
import com.cx.auth.model.vo.UserDetailsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.*;

/**
 * @author zhoujunming
 * @Description TODO
 * @date 2022/6/9 15:30
 * @email zhoujunming@bluemoon.com.cn
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class TestController {
    @Autowired
    private LoginUserHolder loginUserHolder;

    @Autowired
    private TestService testService;

    @PostMapping("/add")
    public String add(@Validated SysUser sysUser){
        UserDetailsVO user= loginUserHolder.getCurrentUser();
        log.info(JSON.toJSONString(user));
        return "add";
    }

    @PostMapping("/update")
    public String update(){
        return "update";
    }

    @PostMapping("/delete")
    public String delete(){
        return "delete";
    }

    @PostMapping("/query")
    public String query(){
        return "query";
    }

    @GetMapping("/test")
    public String test(){
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        FutureTask futureTask = new FutureTask(new Callable<String>() {

            @Override
            public String call() throws Exception {
                testService.test1();
                return "";
            }
        });
        executorService.submit(futureTask);
        testService.test();
        try {
            futureTask.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        return "query";
    }
}
